package com.example.translate;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

@RestController
public class TranslateController {
    @Resource
    TranslateService translateService;

    @GetMapping("/translate")
    public ModelAndView index() {
        return new ModelAndView("index");
    }

    @GetMapping("/translate/search")
    public Map<String, Object> search(Pageable pageable, String content) throws IOException {
        return translateService.search(pageable, content);
    }

    @DeleteMapping("/translate/delete")
    public void delete(String id) throws IOException {
        translateService.delete(id);
    }

    @DeleteMapping("/translate/deleteByContent")
    public void deleteByContent(String en, String cn) throws IOException {
        translateService.delete(en, cn);
    }

    @PostMapping("/translate")
    public Page<Map<String, Object>> translate(@RequestBody TranslateDTO dto, Pageable pageable) throws IOException {
        return translateService.translate(pageable, dto);
    }

    @PostMapping("/translate/save")
    public void save(@RequestBody TranslateDTO dto) throws IOException {
        translateService.save(dto);
    }
}
